<?php
	class PostAction extends Action{
		function __construct(){
			parent::__construct();
			A('User')->Auto();//jugg
		}
		function send(){//ajax send
			if(!$_POST['title']){
				$this->error("请填写标题！");
				return;
			}
			if(strlen($_POST['miao'])>510||strlen($_POST['title'])>200){
				$this->error("简介或者标题字数过多！");
				return;
			}
			$tag=preg_replace('/\s+/', ' ', $_POST['tag']);
			$_POST['tag']=explode(" ", $tag);
			$_POST['tag']=json_encode($_POST['tag']);
			if($_FILES['image']['name']){
				import('ORG.Net.UploadFile');
				$imageinfo=getimagesize($_FILES['image']['tmp_name']);
				$h=$imageinfo[0];
				$w=$imageinfo[1];
				$up=new UploadFile();
				$up->maxSize=30241024;
				$up->allowExts=array('jpg','gif','png','jpeg');
				$up->savePath='./Uploads/image/';
				$up->thumb='true';
				$up->thumbMaxWidth="480";
				$up->thumbMaxHeight="270";
				$up->thumbPath='./Uploads/image/';
				$up->thumbPrefix='min_';
				if(!$up->upload()){
					 $this->error($upload->getErrorMsg());
					 return;
				}else{
					$im=$up->getUploadFileInfo();
					$_POST['image']=substr($im[0]['savepath']."min_".$im[0]['savename'],1);	
				}
			}

			$pid=D('Post')->send($_POST);
			$this->success("发布成功","/index.php/Index/post/pid/".$pid);

		}
		function edit(){
				$pid=(int)$_POST['pid'];
				$jugg=D('Post')->mypost($pid);
				if($jugg['z']=='success'){
					if($_FILES['image']['name']){
						import('ORG.Net.UploadFile');
						$imageinfo=getimagesize($_FILES['image']['tmp_name']);
						$h=$imageinfo[0];
						$w=$imageinfo[1];
						$up=new UploadFile();
						$up->maxSize=30241024;
						$up->allowExts=array('jpg','gif','png','jpeg');
						$up->savePath='./Uploads/image/';
						$up->thumb='true';
						$up->thumbMaxWidth="480";
						$up->thumbMaxHeight="270";
						$up->thumbPath='./Uploads/image/';
						$up->thumbPrefix='min_';
						if(!$up->upload()){
					 		$this->error($upload->getErrorMsg());
					 		return;
						}else{
							$im=$up->getUploadFileInfo();
							$_POST['image']=substr($im[0]['savepath']."min_".$im[0]['savename'],1);	
						}
					}
					$tag=preg_replace('/\s+/', ' ', $_POST['tag']);
					$_POST['tag']=explode(" ", $tag);
					$_POST['tag']=json_encode($_POST['tag']);
					$back=D('Post')->edit($_POST);
					if($back['z']=="success"){
						$this->success("修改成功！",'/index.php/Index/post/pid/'.$back['re']);
					}
					
				}else{
					$this->error($jugg['re']);
					return;
					
				}
		}
		function mark(){
			$pid=(int)$_GET['pid'];
			$jugg=D('Post')->find($pid);
			if(!$jugg){
				return false;
			}
			$re=D('User')->addmark($pid);
			if($re['z']=="success"){
				D('Post')->where("pid='".$pid."'")->setInc("mark");
			}elseif($re['z']=="error"){
				D('Post')->where("pid='".$pid."'")->setDec("mark");
			}else{
				$re['z']="not";
				$re['com']="请先登入！";
			}
			$this->ajaxReturn($re);
		}
		function delpost(){
			$pid=(int)$_GET['pid'];
			$re=D('Post')->mypost($pid);
			if($re['z']=='success'){
				$post=$re['re'];
				D('Post')->delete($pid);

			}
		}

	}